'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    ------
    
    About: Rig tools window. This module builds the window and its
           behaviors.
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import masdevallia.settings.PathConstants as PathConstants
import masdevallia.utility.WindowUtility as WinUtility

import masdevallia.windows.BuildModules as BuildModules
import masdevallia.windows.EditModules as EditModules
import masdevallia.windows.BuildRigs as BuildRigs
import masdevallia.windows.RigLibrary as RigLibrary

from PySide import QtCore
from PySide import QtGui

'''
============================================================
--->>   GLOBAL VARIABLES
============================================================
'''
form_class, base_class = WinUtility.loadUiType( PathConstants.WINDOW_RIG_TOOLS_GUI )

'''
============================================================
--->>   RIG TOOLS CLASS
============================================================
'''
class RigTools( form_class, base_class ):
    def __init__( self, parent=None ):
        super( RigTools, self).__init__( parent )
        self.setupUi( self )
        self.setWindowFlags( QtCore.Qt.Window )
        self.setObjectName( 'Rig_Tools_MainWindow' )
        
        self.dock = None #Name of the docked window.
        self.previous_mode = None
        self.current_mode = 'modules' #Tracks the current mode of the window.
        
        self.initUI()
        
    def initUI( self ):
        '''
        Build custom GUI elements.
        '''
        toggle_left_CSS = '''
        QToolButton {
            background-color: rgb(90,90,90);
            border: 1px solid rgb(42,42,42);
            border-radius: 0px;
            border-top-left-radius: 10px;
        }
        QToolButton:checked {
            color: rgb(42,42,42);
            font-weight: bold;
            background-color: rgb(160,160,160);
            border: 1px solid rgb(42,42,42);
            border-radius: 0px;
            border-top-left-radius: 10px;
        }'''
        
        toggle_center_CSS = '''
        QToolButton {
            background-color: rgb(90,90,90);
            border: 1px solid rgb(42,42,42);
            border-radius: 0px;
        }
        QToolButton:checked {
            color: rgb(42,42,42);
            font-weight: bold;
            background-color: rgb(160,160,160);
            border: 1px solid rgb(42,42,42);
            border-radius: 0px;
        }'''
        
        toggle_right_CSS = '''
        QToolButton {
            background-color: rgb(90,90,90);
            border: 1px solid rgb(42,42,42);
            border-radius: 0px;
            border-top-right-radius: 10px;
        }
        QToolButton:checked {
            color: rgb(42,42,42);
            font-weight: bold;
            background-color: rgb(160,160,160);
            border: 1px solid rgb(42,42,42);
            border-radius: 0px;
            border-top-right-radius: 10px;
        }'''
        
        self.build_modules.setStyleSheet( toggle_left_CSS )
        self.edit_modules.setStyleSheet( toggle_center_CSS )
        self.build_rigs.setStyleSheet( toggle_center_CSS )
        self.rig_library.setStyleSheet( toggle_right_CSS )
        
        # Add the mode widgets to the mode layout. Show the default mode and hide
        # the others.
        self.widget_build_module = BuildModules.BuildModules()
        self.mode_layout.addWidget( self.widget_build_module )
        
        self.widget_edit_module = EditModules.EditModules()
        self.mode_layout.addWidget( self.widget_edit_module )
        self.widget_edit_module.hide()
        
        self.widget_build_rig = BuildRigs.BuildRigs()
        self.mode_layout.addWidget( self.widget_build_rig )
        self.widget_build_rig.hide()
        
        self.widget_rig_library = RigLibrary.RigLibrary()
        self.mode_layout.addWidget( self.widget_rig_library )
        self.widget_rig_library.hide()
        
        # Do connections.
        self.connections()
        
    def connections( self ):
        '''
        Do the connects for the GUI.
        '''
        # Toggle buttons
        self.build_modules.clicked.connect( lambda b='modules':self.manageToggleButtons(b) )
        self.edit_modules.clicked.connect( lambda b='edit':self.manageToggleButtons(b) )
        self.build_rigs.clicked.connect( lambda b='rigs':self.manageToggleButtons(b) )
        self.rig_library.clicked.connect( lambda b='library':self.manageToggleButtons(b) )

        
    # TOGGLE BUTTONS
    def manageToggleButtons( self, button ):
        '''
        Only one toggle button can be active at a time. This method manages that
        behavior.
        '''
        # Hide the current layout.
        self.previous_mode = self.current_mode
        
        self.hideWidgets()
        
        if button == 'modules':
            self.current_mode = 'modules'
            self.build_modules.setChecked( True )
            self.edit_modules.setChecked( False )
            self.build_rigs.setChecked( False )
            self.rig_library.setChecked( False )

        if button == 'edit':
            self.current_mode = 'edit'
            self.build_modules.setChecked( False )
            self.edit_modules.setChecked( True )
            self.build_rigs.setChecked( False )
            self.rig_library.setChecked( False )
            
        elif button == 'rigs':
            self.current_mode = 'rigs'
            self.build_modules.setChecked( False )
            self.edit_modules.setChecked( False )
            self.build_rigs.setChecked( True )
            self.rig_library.setChecked( False )
            
        elif button == 'library':
            self.current_mode = 'library'
            self.build_modules.setChecked( False )
            self.edit_modules.setChecked( False )
            self.build_rigs.setChecked( False )
            self.rig_library.setChecked( True )

        # Update the mode widgets visibility.            
        self.showWidgets()
        
    def showWidgets( self ):
        '''
        Shows the mode widget for the active window mode.
        '''
        if self.current_mode == 'modules':
            self.widget_build_module.show()
        
        elif self.current_mode == 'edit':
            self.widget_edit_module.show()
            
        elif self.current_mode == 'rigs':
            self.widget_build_rig.show()
            
        elif self.current_mode == 'library':
            self.widget_rig_library.show()
            
    def hideWidgets( self ):
        '''
        Hides all inactive mode widgets.
        '''
        if self.previous_mode == 'modules':
            self.widget_build_module.hide()
            
        elif self.previous_mode == 'edit':
            self.widget_edit_module.hide()
            
        elif self.previous_mode == 'rigs':
            self.widget_build_rig.hide()
            
        elif self.previous_mode == 'library':
            self.widget_rig_library.hide()

        
        